package com.sina.shop.api;

import com.sina.shop.domain.collect.CollectBusiness;
import com.sina.shop.service.collect.CollectBusinessService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-02-08-9:39
 */
@RestController
@RequestMapping("collectBusiness")
public class CollectBusinessController extends BaseController {

    @Autowired
    @Qualifier("collectBusinessService")
    private CollectBusinessService collectBusinessService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/getCollectBusinessByAdminId", method = RequestMethod.GET)
    public List<CollectBusiness> getCollectBusinessByAdminId(@RequestParam(value = "adminId") Long adminId) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getCollectBusinessByAdminId, method: get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return collectBusinessService.getCollectBusinessByAdminId(adminId);
    }

    @RequestMapping(value = "/getCollectBusinessByAidAndBid", method = RequestMethod.GET)
    public CollectBusiness getCollectBusinessByAidAndBid(Long adminId, Long businessId) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getCollectBusinessByAidAndBid, method: get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return collectBusinessService.getCollectBusinessByAidAndBid(adminId, businessId);
    }

    @RequestMapping(value = "/deleteCollectBusiness", method = RequestMethod.POST)
    public Integer deleteCollectBusiness(Long id) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/deleteCollectBusiness, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return collectBusinessService.deleteCollectBusiness(id);
    }

    @RequestMapping(value = "/saveCollectBusiness", method = RequestMethod.POST)
    public CollectBusiness saveCollectBusiness(CollectBusiness collectBusiness) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/saveCollectBusiness, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return collectBusinessService.saveCollectBusiness(collectBusiness);
    }
}
